import { _decorator, BoxCollider2D, Collider2D, Component, Contact2DType, IPhysics2DContact, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends Component {

    protected onEnable(): void {

        let boxColider2D = this.node.getComponent(BoxCollider2D);
        boxColider2D.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
        boxColider2D.on(Contact2DType.END_CONTACT, this.onEndContact, this)

    }

    protected onDisable(): void {

        let boxColider2D = this.node.getComponent(BoxCollider2D);
        boxColider2D.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
        boxColider2D.off(Contact2DType.END_CONTACT, this.onEndContact, this)

    }

    protected onLoad(): void {

    }

    start() {

    }

    update(deltaTime: number) {

    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        if (otherCollider.node.name === 'Bullet01') {
            otherCollider.node.destroy();
            this.node.destroy();
        }
    }

    onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        console.log('onEndContact   ' + otherCollider.node.name);
    }
}


